package info.hexin.aop;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;

public class Aop {
    public static void main(String[] args) throws IOException, IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException {
        
        ClassReader cr = new ClassReader(Foo.class.getName());
        ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
        ClassVisitor cv = new MethodChangeClassAdapter(cw);
        cr.accept(cv, Opcodes.ASM4);

        // gets the bytecode of the Example class, and loads it dynamically
        byte[] code = cw.toByteArray();
//
        AopClassLoader loader = new AopClassLoader();
//        Class<?> exampleClass = loader.defineClass(code);
        Class<?> exampleClass = loader.defineClass(Foo.class.getName(), code);


//        for (Method method : exampleClass.getMethods()) {
//            System.out.println(method);
//        }
        
//        exampleClass.getMethods()[0].invoke(null, null); // 調用execute，修改方法內容

        // gets the bytecode of the Example class, and loads it dynamically
//        FileOutputStream fos = new FileOutputStream("e:\\logs\\Example.class");
//        fos.write(code);
//        fos.close();
    }
}
